package com.xoa.controller.workflow;

import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowRule;
import com.xoa.service.workflow.flowRule.FlowRuleService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gsb on 2017/8/21.
 */
@Controller
@RequestMapping("/flowRule")
public class FlowRuleController {

    private Logger loger = LoggerFactory.getLogger(FlowRuleController.class);

    @Resource
    private FlowRuleService flowRuleService;

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 16:52
     * 方法介绍:   查询工作委托的所有数据
     * 参数说明:
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryFlowRule")
    public ToJson<FlowRule> queryFlowRule(@RequestParam(required = false) Integer page,
                                          @RequestParam(required = false) Integer pageSize,
                                          @RequestParam(required = false) boolean useFlag) {
        return flowRuleService.queryFlowRule(page, pageSize, useFlag);
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:10
     * 方法介绍:   添加一条工作委托
     * 参数说明:
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertFlowRule", produces = {"application/json;charset=UTF-8"})
    public ToJson<FlowRule> insertFlowRule(FlowRule flowRule, HttpServletRequest request) {
        if(flowRule.getBeginDate()!=null){
            if(flowRule.getBeginDate().equals("")){
                flowRule.setBeginDate(null);
            }
        }
        if(flowRule.getEndDate()!=null) {
            if (flowRule.getEndDate().equals("")) {
                flowRule.setEndDate(null);
            }
        }
        return flowRuleService.insertFlowRule(flowRule,request);
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:20
     * 方法介绍:   修改工作委托的状态为开始=“1”
     * 参数说明:
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatusToStart", produces = {"application/json;charset=UTF-8"})
    public ToJson<FlowRule> updateStatusToStart(@RequestParam(value = "ruleId[]",required = false) Integer[] ruleId, HttpServletRequest request) {
        return flowRuleService.updateStatusToStart(ruleId);
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:30
     * 方法介绍:   修改工作委托的状态为关闭=“0”
     * 参数说明:
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatusToEnd", produces = {"application/json;charset=UTF-8"})
    public ToJson<FlowRule> updateStatusToEnd(@RequestParam(value = "ruleId[]",required = false) Integer[] ruleId) {
        return flowRuleService.updateStatusToEnd(ruleId);
    }
    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/21 17:20
     * 方法介绍:   删除工作委托
     * 参数说明:
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteFlowRule", produces = {"application/json;charset=UTF-8"})
    public ToJson<FlowRule> deleteFlowRule(@RequestParam(value = "ruleId[]",required = false) Integer[] ruleId) {
        return flowRuleService.deleteFlowRule(ruleId);
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017/8/23 16:20
     * 方法介绍:   根据流程名称，委托人姓名，被委托人姓名查询工作委托
     * 参数说明:
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectFlowRule")
    public ToJson<FlowRule> selectFlowRule(@RequestParam(required = false) Integer page,
                                           @RequestParam(required = false) Integer pageSize,
                                           @RequestParam(required = false) boolean useFlag,
                                            @RequestParam(required = false,defaultValue = "null" ) String flowId,
                                            @RequestParam(required = false) Integer idStatus,
                                            @RequestParam(required = false,defaultValue = "null") String userId,
                                            @RequestParam(required = false,defaultValue = "null") String toId, HttpServletRequest request) {

        Map maps=new HashMap();
        maps.put("flowId",flowId);
        maps.put("idStatus",idStatus);
        maps.put("userId",userId);
        maps.put("toId",toId);
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        maps.put("pageParams",pageParams);
        //如果前台传入的不是其他
        if (idStatus != null) {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            maps.put("userId",user.getUserId());
            return flowRuleService.selectFlowRule(maps);
        }
        return flowRuleService.selectFlowRule(maps);
    }

    /**
     * @创建作者:李然 Lr
     * @方法描述：显示编辑
     * @创建时间：18:31 2018/7/23
     **/
    @ResponseBody
    @RequestMapping("/selectFlowRuleById")
    public ToJson selectFlowRuleByFlowId(Integer ruleId) {
        return flowRuleService.selectFlowRuleById(ruleId);
    }


    /**
    * @创建作者:李然  Lr
    * @方法描述：编辑
    * @创建时间：18:34 2018/7/24

    **/
    @ResponseBody
    @RequestMapping("/updateFlowRule")
    public ToJson updateFlowRule(FlowRule flowRule,HttpServletRequest request) {
        return flowRuleService.updateFlowRule(flowRule,request);
    }

}